﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using TreaShares.Backoffice.Controlers;

namespace TreaShares.Login
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string clientName = this.Page.RouteData.Values["ClientName"] as string;
        }

        protected void SetCookies()
        {
            string clientName = this.Page.RouteData.Values["ClientName"] as string;
            Response.Cookies["OrganisationId"].Value = UsersManager.GetClientId(clientName).ToString();
            Response.Cookies["OrganisationId"].Expires = DateTime.Now.AddDays(5);
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            string password = this.Password.Text;
            string userName = this.UserName.Text;
            bool valid = Membership.ValidateUser(userName, password);   
            if (valid)
            {
                this.LoginUser(userName);
                if (Roles.IsUserInRole(this.UserName.Text, "Administrator"))
                {
                    Response.Redirect("~/Backoffice/Administrator/Clients.aspx");
                }
                if (Roles.IsUserInRole(this.UserName.Text, "PowerUser"))
                {
                    Response.Redirect("~/Backoffice/User/Transactions.aspx");
                }
                if (Roles.IsUserInRole(this.UserName.Text, "User"))
                {
                    Response.Redirect("~/Backoffice/User/Transactions.aspx");
                }
            }
            else
            {
                this.FailureText.Text = "User name or password are incorect";
            }
           
        }
        private void LoginUser(string userName)
        {
             
            // Create the cookie that contains the forms authentication ticket
            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, false);

            // Get the FormsAuthenticationTicket out of the encrypted cookie
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

            // Create a new FormsAuthenticationTicket that includes our custom User Data
            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate,
                ticket.Expiration, ticket.IsPersistent, userName);

            // Update the authCookie's Value to use the encrypted version of newTicket
            authCookie.Value = FormsAuthentication.Encrypt(newTicket);

            //' Manually add the authCookie to the Cookies collection 
            Response.Cookies.Add(authCookie);

            Response.Cookies["OrganisationId"].Value = UsersManager.GetClientId(userName).ToString();
            Response.Cookies["OrganisationId"].Expires = DateTime.Now.AddDays(5);
        }

    }
}